package likou.duoxianc;

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2021/10/20 08:52
 * <p>
 * <p>
 * 如果，A事务，select 为100，加100.
 * B事务，select为100，也加100
 * 则A，B事务的提交是什么状态。
 */
public class _abc {
    static int p = 1;

    public static void main(String[] args) {
        _abc t = new _abc();
//        t.reentrant();
//        t.waita();
//        t.semaphore();
    }


    private void semaphore() {
        Semaphore semaphore1 = new Semaphore(1);
        Semaphore semaphore2 = new Semaphore(0);
        Semaphore semaphore3 = new Semaphore(0);
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    semaphore1.acquire();
                    System.out.println("a");
                    semaphore2.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    semaphore2.acquire();
                    System.out.println("b");
                    semaphore3.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    semaphore3.acquire();
                    System.out.println("c");
                    semaphore1.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void waita() {
        Object o = new Object();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (o) {
                    try {
                        while (p != 1) {
                            o.wait();
                        }
                        System.out.println('a');
                        p = 2;
                        o.notifyAll();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (o) {
                    try {
                        while (p != 2) {
                            o.wait();
                        }
                        System.out.println('b');
                        p = 3;
                        o.notifyAll();

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (o) {
                    try {
                        while (p != 3) {
                            o.wait();
                        }
                        System.out.println('c');
                        p = 1;
                        o.notifyAll();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    private void reentrant() {
        ReentrantLock lock = new ReentrantLock();
        Condition c1 = lock.newCondition();
        Condition c2 = lock.newCondition();
        Condition c3 = lock.newCondition();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                lock.lock();
                try {
                    if (p != 1) {
                        c1.await();
                    }
                    System.out.println('a');
                    p = 2;
                    c2.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                lock.lock();
                try {
                    if (p != 2) {
                        c2.await();
                    }
                    System.out.println('b');
                    p = 3;
                    c3.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                lock.lock();
                try {
                    if (p != 3) {
                        c3.await();
                    }
                    System.out.println('c');
                    p = 1;
                    c1.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }).start();
    }
}
